package org.effectiveframework.xml;

import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This Utility class contains some utility method for xml
 *
 * @author Suraj Chhetry <suraj@drose.com.np>
 * @since 0.1
 * @version 0.1
 */
public class XmlUtility {

    /**
     * *
     * This method extract node value from xml string
     *
     * @param xml source xml string
     * @param tag whose value need to find
     * @return list of value of found otherwise empty collection
     */
    public static Collection<String> extractValue(String xml, String tag) {
        Pattern patternTag;
        Matcher matcherTag;
        Collection<String> data = new ArrayList<String>();
        final String HTML_A_TAG_PATTERN = "(?i)" + "<" + tag + ">(.+?)</" + tag + ">";
        patternTag = Pattern.compile(HTML_A_TAG_PATTERN);
        matcherTag = patternTag.matcher(xml);
        while (matcherTag.find()) {
            data.add(matcherTag.group(1));
        }
        return data;
    }
}
